package com.songqi.common.core.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.songqi.common.core.constant.HttpStatus;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @description: 统一返回数据类型
 * @author: songqi(13776346982)
 * @date: 2022/6/28 23:25
 */
@Data
public class R<T> {


    @ApiModelProperty("返回码。200-正常，500-业务错误，401-未授权")
    private Integer code;

    /**
     * 失败信息
     */
    @ApiModelProperty("失败信息。具体的失败信息接口都有明确的返回")
    private String msg;

    /**
     * 请求处理成功或者失败标志。true：处理成功。false：处理失败
     */
    @ApiModelProperty("请求处理成功或者失败标志。true：处理成功。false：处理失败")
    private Boolean success;

    /**
     * 返回数据
     */
    private T data;


    @ApiModelProperty("返回描述中占位符参数值")
    @JsonIgnore
    private Object[] messageParams;

    public static <T> R <T> success(T data){
        return success(HttpStatus.SUCCESS, "操作成功", data);
    }


    public static <T> R <T> success(){
        return success(HttpStatus.SUCCESS, "操作成功", null);
    }
    public static <T> R <T> success(Integer code, String msg, T data){
        R r = new R();
        r.setSuccess(true);
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);

        return r;
    }


    public static <T> R <T> error(){
        return error(HttpStatus.ERROR, "操作失败", null, null);
    }

    public static <T> R <T> error(String msg){
        return error(HttpStatus.ERROR, msg, null, null);
    }

    public static <T> R <T> error(String msg, Object[] messageParams){
        return error(HttpStatus.ERROR, msg, null, messageParams);
    }

    public static <T> R <T> error(Integer code, String msg){
        return error(code, msg, null, null);
    }

    public static <T> R <T> error(Integer code, String msg, T data, Object[] messageParams){
        R r = new R();
        r.setSuccess(false);
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        r.setMessageParams(messageParams);
        return r;
    }

    public static <T> R<T> ok()
    {
        return restResult(null, HttpStatus.SUCCESS, null, true);
    }

    public static <T> R<T> ok(T data)
    {
        return restResult(data, HttpStatus.SUCCESS, null, true);
    }

    public static <T> R<T> ok(T data, String msg)
    {
        return restResult(data, HttpStatus.SUCCESS, msg, true);
    }

    public static <T> R<T> fail()
    {
        return restResult(null, HttpStatus.ERROR, null, false);
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null, HttpStatus.ERROR, msg, false);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data, HttpStatus.ERROR, null, false);
    }

    public static <T> R<T> fail(T data, String msg)
    {
        return restResult(data, HttpStatus.ERROR, msg, false);
    }

    public static <T> R<T> fail(int code, String msg)
    {
        return restResult(null, code, msg, false);
    }

    private static <T> R<T> restResult(T data, int code, String msg, boolean success)
    {
        R<T> apiResult = new R<T>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setSuccess(success);
        return apiResult;
    }
}
